package com.chuanyang.driverh.mainhome;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainhome.adapter.GoodsForAssignWaybilDetailAdapter2;
import com.chuanyang.driverh.response2.ResultAllotGoodsSuccess;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class WaybilDetailForCarPlanActivity extends BaseTitleActivity {

    private TextView tv_waybill_no;
    private TextView tv_date;
    private TextView tv_driver_name;
    private TextView tv_plate_no;
    private TextView tv_order_status;
    private TextView tv_tel;
    private TextView tv_tel_dial;
    private TextView tv_gua_weight;
    private TextView tv_shi_weight;
    private TextView tv_gua_num;
    private TextView tv_shi_num;
    private TextView tv_all_price;
    private ListView lv_goods;
    private TextView tv_confirm;
    private LinearLayout ll_photo_out;
    private LinearLayout ll_photo;
    private Dialog dialog;

    private String waybilId;
    //0,消息进入，不显示按钮和状态，2派单 查看派单-运单详情，不显示状态,//3,代运实发-查看派单-运单列表，显示运输校核 ,4代运实发运单地图查看
    private int type;//此页面专为车辆安排查看派单，进入运单详情。
    @Override
    protected int getContentResId() {
        return R.layout.home_act_waibil_detail_no_top;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        View view=View.inflate(activityContext,R.layout.home_item_waybil_top,null);

        tv_waybill_no=(TextView)view.findViewById(R.id.tv_waybill_no);
        tv_date=(TextView)view.findViewById(R.id.tv_date);
        tv_driver_name=(TextView)view.findViewById(R.id.tv_driver_name);
        tv_plate_no=(TextView)view.findViewById(R.id.tv_plate_no);
        tv_order_status=(TextView)view.findViewById(R.id.tv_order_status);
        tv_tel=(TextView)view.findViewById(R.id.tv_tel);
        tv_tel_dial=(TextView)view.findViewById(R.id.tv_tel_dial);
        tv_gua_weight=(TextView)view.findViewById(R.id.tv_gua_weight);
        tv_shi_weight=(TextView)view.findViewById(R.id.tv_shi_weight);
        tv_gua_num=(TextView)view.findViewById(R.id.tv_gua_num);
        tv_shi_num=(TextView)view.findViewById(R.id.tv_shi_num);
        tv_all_price=(TextView)view.findViewById(R.id.tv_all_price);
        ll_photo_out=(LinearLayout)view. findViewById(R.id.ll_photo_out);
        ll_photo=(LinearLayout) view.findViewById(R.id.ll_photo);


        tv_confirm=(TextView)findViewById(R.id.tv_confirm);
        lv_goods=(ListView)findViewById(R.id.lv_goods);
        lv_goods.addHeaderView(view);
    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("运单详情");
                getRightTextView().setText("状态");
        waybilId=getIntent().getStringExtra("waybilId");
        type=getIntent().getIntExtra("type",0);
        if (type==0) {
            tv_confirm.setVisibility(View.GONE);
            getRightTextView().setVisibility(View.GONE);
        }else if(type==1){
            tv_confirm.setVisibility(View.GONE);
            getRightTextView().setVisibility(View.GONE);
        }else if(type==2){
            tv_confirm.setVisibility(View.GONE);
            getRightTextView().setVisibility(View.GONE);
        }else if(type==3){
            tv_confirm.setVisibility(View.VISIBLE);
            getRightTextView().setVisibility(View.VISIBLE);
        }else if(type==4){
            tv_confirm.setVisibility(View.VISIBLE);
            getRightTextView().setVisibility(View.VISIBLE);
        }
        getTopData();
    }



    private void getTopData() {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("driverId", SharedPrefUtil.getUserId());
//        params.put("page", mCurrentPage + "");
        params.put("waybillId", waybilId);

        MyHttpUtil.getWaybillInfoByWaybillId(activityContext,params, new MyCallback<ResultAllotGoodsSuccess>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(ResultAllotGoodsSuccess result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    try {
                        updateUi(result.getData());
                        if (result.getData()!=null && result.getData().getWaybillGoodsInfos()!=null &&result.getData().getWaybillGoodsInfos().size()>0 )
                        setGoodsAdapter(result.getData().getWaybillGoodsInfos());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
//                    ResultAllotGoodsSuccess.DataBean ss = result.getData();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }


    private void setGoodsAdapter(List<ResultAllotGoodsSuccess.DataBean.WaybillGoodsInfosBean> data) {
        GoodsForAssignWaybilDetailAdapter2 adapter=new GoodsForAssignWaybilDetailAdapter2(activityContext,data);
        lv_goods.setAdapter(adapter);
    }

    private void updateUi(final ResultAllotGoodsSuccess.DataBean waybildata) {
        if (waybildata==null)return;
        tv_waybill_no.setText(waybildata.getWaybillNo());
        tv_date.setText(waybildata.getCreatetime());
        tv_driver_name.setText(waybildata.getUsername());
        tv_plate_no.setText(waybildata.getPlateNumber());
        tv_tel.setText(StringUtil.handlePhoneNumShow(waybildata.getPhone()));


        tv_gua_weight.setText(waybildata.getOriginalWeight()+"");
        if (waybildata.getSumDetailsWeight()!=0)
        tv_shi_weight.setText(waybildata.getSumDetailsWeight()+"");
        tv_gua_num.setText(waybildata.getOriginalNum()+"");
        if (waybildata.getSumDetailsNum()!=0)
        tv_shi_num.setText(waybildata.getSumDetailsNum()+"");
        tv_all_price.setText(waybildata.getListedPrice()+"");

        ll_photo_out.setVisibility(View.GONE);

       if (waybildata.getWaybillStatus()==2){
            tv_order_status.setText("待装货");
        }else if (waybildata.getWaybillStatus()==3){
            tv_order_status.setText("装货中");
        }else if (waybildata.getWaybillStatus()==4){
            tv_order_status.setText("运输中");
        }else if (waybildata.getWaybillStatus()==5){
            tv_order_status.setText("卸货中");
        }else if (waybildata.getWaybillStatus()==6){
            tv_order_status.setText("待收货");
        }else if (waybildata.getWaybillStatus()==7){
            tv_order_status.setText("已完成");
        }else if (waybildata.getWaybillStatus()==8){
            tv_order_status.setText("已取消");
        }
        tv_tel_dial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.dial(activityContext,waybildata.getPhone());
            }
        });




    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void baseGoEnsure() {
        Intent intent = new Intent(activityContext, WaybilStatusActivity.class);
        intent.putExtra("waybilId", waybilId + "");
        startActivity(intent);
    }


}
